package com.verlet.springbootdemo.handler;

import com.verlet.springbootdemo.common.ServerResponse;
import com.verlet.springbootdemo.execption.BizException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 *
 * @author verlet
 * @date 2021-02-27 01:09
 */
@ControllerAdvice("com.verlet.springbootdemo.controller")
public class GlobalExceptionHandler {

    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public ServerResponse<Void> bizExceptionHandler(BizException e) {
        return ServerResponse.createByErrorCodeMessage(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ServerResponse<Void> exceptionHandler(Exception e) {
        return ServerResponse.createByErrorMessage(e.getMessage());
    }
}
